home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 41.0 KB | 1,482 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWBitmap.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWBITMAP_H
- #include "FWBitmap.h"
- #endif
-
- #ifndef FWGRGLOB_H
- #include "FWGrGlob.h"
- #endif
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #ifndef FWCOLOR_H
- #include "FWColor.h"
- #endif
-
- #ifndef FWGC_H
- #include "FWGC.h"
- #endif
-
- #ifndef FWGDEV_H
- #include "FWGDev.h"
- #endif
-
- #ifndef FWACQUIR_H
- #include "FWAcquir.h"
- #endif
-
- #ifndef FWODGEOM_H
- #include "FWODGeom.h"
- #endif
-
- #ifndef FWGRUTIL_H
- #include "FWGrUtil.h"
- #endif
-
- #ifndef FWWINRES_H
- #include "FWWinRes.h"
- #endif
-
- #ifndef FWPALETE_H
- #include "FWPalete.h"
- #endif
-
- #if defined(FW_BUILD_WIN) && !defined(FWWINDIB_H)
- #include "FWWinDIB.h"
- #endif
-
- #if defined(FW_BUILD_WIN) && !defined(FWGRUTIL_H)
- #include "FWGrUtil.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(FWRESACC_H)
- #include "FWResAcc.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(FWPICTUR_H)
- #include "FWPictur.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(FWRESSIN_H)
- #include "FWResSin.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__PICTUTILS__)
- #include "PictUtils.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- //========================================================================================
- // Local helpers
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- void MacRepackResourceRow(char* curBuffer, short bitDepth, short resRowBytes);
- void MacRepackValue(short* s);
- void MacRepackValue(unsigned short* s);
- void MacRepackValue(long* l);
- void MacRepackValue(unsigned long* l);
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphics_Bitmap
- #endif
-
- FW_DEFINE_CLASS_M1(FW_PBitmap, FW_CGraphicCountedPtr)
- FW_DEFINE_CLASS_M1(FW_CBitmapRep, FW_CGraphicCountedPtrRep)
-
- FW_REGISTER_ARCHIVABLE_CLASS(FW_LBitmapRep, FW_CBitmapRep, FW_CBitmapRep::Read, FW_CGraphicCountedPtrRep::Write)
-
- //========================================================================================
- // class FW_PBitmap
- //========================================================================================
-
- // #define FW_SHOW_CTABS // Show color tables in a window
-
- //----------------------------------------------------------------------------------------
- // FW_PBitmap::FW_PBitmap
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap::FW_PBitmap() :
- FW_CGraphicCountedPtr()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PBitmap::FW_PBitmap
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap::FW_PBitmap(short width, short height, short pixelSize, FW_Palette palette /* = NULL */) :
- FW_CGraphicCountedPtr()
- {
-
- #ifdef FW_BUILD_MAC
- FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
- pixelSize == 8 || pixelSize == 16 || pixelSize == 32);
- #endif
- #ifdef FW_BUILD_WIN
- FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
- pixelSize == 8 || pixelSize == 16 || pixelSize == 24);
- #endif
-
- SetRep(new FW_CBitmapRep(width, height, pixelSize, palette));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PBitmap::FW_PBitmap
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap::FW_PBitmap(void* image, unsigned long imageSize,
- short width, short height, short pixelSize, FW_Palette palette /* = NULL */) :
- FW_CGraphicCountedPtr()
- {
- FW_ASSERT(image != 0);
-
- #ifdef FW_BUILD_MAC
- FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
- pixelSize == 8 || pixelSize == 16 || pixelSize == 32);
- #endif
- #ifdef FW_BUILD_WIN
- FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
- pixelSize == 8 || pixelSize == 16 || pixelSize == 24);
- #endif
-
- SetRep(new FW_CBitmapRep(image, imageSize, width, height, pixelSize, palette));
- }
-
- #ifdef FW_BUILD_MAC
-
- const FW_CRect FW_PBitmap::gWholePicture = FW_CRect();
-
- //----------------------------------------------------------------------------------------
- // FW_PBitmap::FW_PBitmap
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap::FW_PBitmap(FW_PPicture picture, const FW_CColor& fillColor, const FW_CRect& pictPart) :
- FW_CGraphicCountedPtr()
- {
- FW_ASSERT((const void*)picture != NULL);
-
- FW_CRect bounds = pictPart;
- if(&pictPart == &gWholePicture)
- picture->GetPictBounds(bounds);
-
- SetRep(new FW_CBitmapRep(picture, fillColor, bounds));
- }
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PBitmap::FW_PBitmap
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap::FW_PBitmap(FW_PlatformBitmap platformBitmap) :
- FW_CGraphicCountedPtr()
- {
- FW_ASSERT(platformBitmap != NULL);
- SetRep(new FW_CBitmapRep(platformBitmap));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PBitmap::FW_PBitmap
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap::FW_PBitmap(FW_CResourceFile& resourceFile, FW_ResourceId resId) :
- FW_CGraphicCountedPtr()
- {
- SetRep(new FW_CBitmapRep(resourceFile, resId));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PBitmap::FW_PBitmap
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap::FW_PBitmap(const FW_PBitmap& other) :
- FW_CGraphicCountedPtr(other)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PBitmap::FW_PBitmap
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap::~FW_PBitmap()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PBitmap::operator=
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap& FW_PBitmap::operator=(const FW_PBitmap& other)
- {
- // We don't need to test this == &other because SetRep will do it
- SetRep(other.GetRep());
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // CalcRowBytes
- //----------------------------------------------------------------------------------------
-
- static short CalcRowBytes(short width, short pixelSize)
- {
- return ((width * pixelSize + 15) / 16) * 2;
- }
-
- //========================================================================================
- // class FW_CBitmapRep
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::FW_CBitmapRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapRep::FW_CBitmapRep(short width, short height, short pixelSize, FW_Palette palette):
- #ifdef FW_BUILD_MAC
- fLockCount(0),
- fPixMapHandle(NULL),
- #endif
- #ifdef FW_BUILD_WIN
- fUserColorDepth(0),
- fWinPalette(NULL),
- #endif
- fPlatformBitmap(NULL),
- fOwnBitmap(FALSE)
- {
- MakePlatformBitmap(width, height, pixelSize, palette);
- fOwnBitmap = TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::FW_CBitmapRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapRep::FW_CBitmapRep(void* image, unsigned long imageSize,
- short width, short height, short pixelSize, FW_Palette palette):
- #ifdef FW_BUILD_MAC
- fLockCount(0),
- fPixMapHandle(NULL),
- #endif
- #ifdef FW_BUILD_WIN
- fUserColorDepth(0),
- fWinPalette(NULL),
- #endif
- fPlatformBitmap(NULL),
- fOwnBitmap(FALSE)
- {
- MakePlatformBitmap(width, height, pixelSize, palette);
- fOwnBitmap = TRUE;
- SetImage(image, imageSize);
- }
-
- #ifdef FW_BUILD_MAC
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::FW_CBitmapRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapRep::FW_CBitmapRep(FW_PPicture picture, const FW_CColor& fillColor, const FW_CRect& pictPart):
- fPlatformBitmap(NULL),
- fOwnBitmap(TRUE),
- fLockCount(0),
- fPixMapHandle(NULL)
- {
- FW_ASSERT((const void*)picture != NULL);
-
- FW_PlatformPict pictHandle = picture->GetPlatformPict();
-
- // Just get picture color depth at first
- PictInfo pictInfo;
- ::GetPictInfo(pictHandle, &pictInfo, 0, 0, 0, 0);
-
- // Now that we know the color depth, we are ready to get the color table
- short nColors = pictInfo.depth > 8 ? 256 : 1 << pictInfo.depth;
- ::GetPictInfo(pictHandle, &pictInfo, returnColorTable, nColors, systemMethod, 0);
-
- #ifdef FW_SHOW_CTABS
- FW_MacShowColorTable(pictInfo.theColorTable);
- #endif
-
- // Create a GWorld
- FW_SPlatformRect gworldRect = pictPart;
- ::OffsetRect(&gworldRect, -gworldRect.left, -gworldRect.top);
- QDErr err = ::NewGWorld(&fPlatformBitmap, pictInfo.depth, &gworldRect,
- pictInfo.theColorTable, NULL, useTempMem);
-
- ::DisposeHandle((Handle)pictInfo.theColorTable);
-
- FW_FailOnError(err);
- if(fPlatformBitmap == NULL)
- FW_Failure(FW_xGraphicException);
-
- // Copy the image
- CGrafPtr curPort;
- GDHandle gdh;
- ::GetGWorld(&curPort, &gdh);
- ::SetGWorld(fPlatformBitmap, NULL);
-
- FW_CMacPixelLock lock(this);
- PixMapHandle pmh = lock.GetPixMapHandle();
-
- ::RGBBackColor(&fillColor);
- ::EraseRect(&gworldRect);
-
- FW_SPlatformRect rectPicture = pictInfo.sourceRect;
- ::OffsetRect(&rectPicture, -pictPart.left.AsInt(), -pictPart.top.AsInt());
- ::DrawPicture(pictHandle, &rectPicture);
-
- ::SetGWorld(curPort, gdh);
-
- #ifdef FW_SHOW_CTABS
- FW_MacShowColorTable((*pmh)->pmTable);
- #endif
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::FW_CBitmapRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapRep::FW_CBitmapRep(FW_PlatformBitmap platformBitmap):
- #ifdef FW_BUILD_MAC
- fLockCount(0),
- fPixMapHandle(NULL),
- #endif
- #ifdef FW_BUILD_WIN
- fUserColorDepth(FW_CPrivWinBitmap::GetColorDepth(platformBitmap)),
- fWinPalette(NULL),
- #endif
- fPlatformBitmap(platformBitmap),
- fOwnBitmap(FALSE)
- {
- }
-
- #ifdef FW_BUILD_MAC
-
- //----------------------------------------------------------------------------------------
- // MacRepackResourceRow
- //----------------------------------------------------------------------------------------
-
- void MacRepackResourceRow(char* curBuffer, short bitDepth, short resRowBytes)
- {
- short pixelBytes = bitDepth / 8;
-
- if(pixelBytes == 2)
- {
- do
- {
- curBuffer[0] = curBuffer[0] ^ curBuffer[1];
- curBuffer[1] = curBuffer[0] ^ curBuffer[1];
- curBuffer[0] = curBuffer[0] ^ curBuffer[1];
- }
- while((resRowBytes -= pixelBytes) > 0);
- }
- else if(pixelBytes == 4)
- {
- do
- {
- curBuffer[0] = curBuffer[0] ^ curBuffer[3];
- curBuffer[3] = curBuffer[0] ^ curBuffer[3];
- curBuffer[0] = curBuffer[0] ^ curBuffer[3];
-
- curBuffer[2] = curBuffer[2] ^ curBuffer[1];
- curBuffer[1] = curBuffer[2] ^ curBuffer[1];
- curBuffer[2] = curBuffer[2] ^ curBuffer[1];
- }
- while((resRowBytes -= pixelBytes) > 0);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // MacRepackValue
- //----------------------------------------------------------------------------------------
-
- void MacRepackValue(short* s)
- {
- char* c = (char*) s;
-
- c[0] = c[0] ^ c[1];
- c[1] = c[0] ^ c[1];
- c[0] = c[0] ^ c[1];
- }
-
- void MacRepackValue(unsigned short* s)
- {
- char* c = (char*) s;
-
- c[0] = c[0] ^ c[1];
- c[1] = c[0] ^ c[1];
- c[0] = c[0] ^ c[1];
- }
-
- void MacRepackValue(long* l)
- {
- char* c = (char*) l;
-
- c[0] = c[0] ^ c[3];
- c[3] = c[0] ^ c[3];
- c[0] = c[0] ^ c[3];
-
- c[2] = c[2] ^ c[1];
- c[1] = c[2] ^ c[1];
- c[2] = c[2] ^ c[1];
- }
-
- void MacRepackValue(unsigned long* l)
- {
- char* c = (char*) l;
-
- c[0] = c[0] ^ c[3];
- c[3] = c[0] ^ c[3];
- c[0] = c[0] ^ c[3];
-
- c[2] = c[2] ^ c[1];
- c[1] = c[2] ^ c[1];
- c[2] = c[2] ^ c[1];
- }
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::FW_CBitmapRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapRep::FW_CBitmapRep(FW_CResourceFile& resourceFile, FW_ResourceId resId):
- #ifdef FW_BUILD_MAC
- fLockCount(0),
- fPixMapHandle(NULL),
- #endif
- #ifdef FW_BUILD_WIN
- fUserColorDepth(0),
- fWinPalette(NULL),
- #endif
- fPlatformBitmap(NULL),
- fOwnBitmap(FALSE)
- {
- FW_CResource resource(resourceFile, resId, FW_kBitmap);
- FW_CResourceSink sink(resource);
- FW_CReadableStream stream(&sink);
-
- #ifdef FW_BUILD_WIN
- ReadFromPlatformFormat(stream, FALSE);
- #endif
- #ifdef FW_BUILD_MAC
- ReadFromPlatformFormat(stream, TRUE);
- #endif
-
- fOwnBitmap = TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::FW_CBitmapRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapRep::FW_CBitmapRep(FW_CReadableStream& archive, FW_Boolean dibFileHeader):
- #ifdef FW_BUILD_MAC
- fLockCount(0),
- fPixMapHandle(NULL),
- #endif
- #ifdef FW_BUILD_WIN
- fUserColorDepth(0),
- fWinPalette(NULL),
- #endif
- fPlatformBitmap(NULL),
- fOwnBitmap(FALSE)
- {
- ReadFromPlatformFormat(archive, dibFileHeader);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::~FW_CBitmapRep
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapRep::~FW_CBitmapRep()
- {
- DisposePlatformBitmap();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::MakePlatformBitmap
- //----------------------------------------------------------------------------------------
- // Can throw an exception
-
- void FW_CBitmapRep::MakePlatformBitmap(short width, short height, short pixelSize, FW_Palette palette)
- {
- FW_PlatformBitmap bitmap = NULL;
-
- if (height != 0 && width != 0)
- {
- #ifdef FW_BUILD_MAC
- FW_SPlatformRect boundsRect(0, 0, width, height);
-
- OSErr err = ::NewGWorld(&bitmap, pixelSize, &boundsRect, palette, NULL, useTempMem);
- FW_FailOnError(err);
- #endif
- #ifdef FW_BUILD_WIN
- // Determine color depth for the DDB
- short bmPlanes = 1, bmBitsPixel = 1;
- if(pixelSize != 1)
- FW_WinPrivGetDisplayColorInfo(bmPlanes, bmBitsPixel);
-
- // Create the bitmap
- bitmap = ::CreateBitmap(width, height, bmPlanes, bmBitsPixel, NULL);
- if(bitmap == NULL)
- FW_Failure(FW_xMemoryExhausted);
-
- // Update the color depth
- if(pixelSize != 0)
- fUserColorDepth = pixelSize;
- else
- fUserColorDepth = FW_CPrivWinBitmap::GetColorDepth(bitmap);
-
- // Copy the palette if there is one
- if (palette != NULL)
- fWinPalette = FW_CopyPalette(palette);
- #endif
- }
-
- fPlatformBitmap = bitmap;
- fOwnBitmap = TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::DisposePlatformBitmap
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::DisposePlatformBitmap()
- {
- if (fOwnBitmap && fPlatformBitmap != NULL)
- {
- #ifdef FW_BUILD_MAC
- ::DisposeGWorld(fPlatformBitmap);
- #endif
- #ifdef FW_BUILD_WIN
- ::DeleteObject(fPlatformBitmap);
-
- if (fWinPalette != NULL)
- {
- ::DeleteObject(fWinPalette);
- fWinPalette = NULL;
- }
- #endif
- }
- fPlatformBitmap = NULL;
- fOwnBitmap = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::SetImage
- //----------------------------------------------------------------------------------------
- // The rowbyte of the image should be the rowbytes of the bitmap
-
- void FW_CBitmapRep::SetImage(void* image, unsigned long imageSize)
- {
- FW_ASSERT(image != NULL);
-
- short bmpHeight, bmpWidth, bmpRowBytes, bmpPixelSize;
- GetBitmapInfo(bmpWidth, bmpHeight, bmpRowBytes, bmpPixelSize);
-
- #ifdef FW_BUILD_MAC
- FW_CMacPixelLock lock(this);
- PixMapHandle pmh = lock.GetPixMapHandle();
- Ptr baseAddr = ::GetPixBaseAddr(pmh);
-
- size_t toCopy = bmpHeight * bmpRowBytes;
- if (toCopy > imageSize)
- toCopy = imageSize;
-
- FW_PrimitiveCopyMemory(image, baseAddr, toCopy);
- #endif
-
- #ifdef FW_BUILD_WIN
- ::SetBitmapBits(fPlatformBitmap, imageSize, image);
- #endif
- }
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::MacLockPixels
- //----------------------------------------------------------------------------------------
-
- PixMapHandle FW_CBitmapRep::MacLockPixels()
- {
- FW_ASSERT(fPlatformBitmap != NULL);
-
- if (fLockCount == 0)
- {
- FW_ASSERT(fPixMapHandle == NULL);
- fPixMapHandle = ::GetGWorldPixMap(fPlatformBitmap);
- ::LockPixels(fPixMapHandle);
- }
-
- fLockCount++;
- return fPixMapHandle;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::MacUnlockPixels
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::MacUnlockPixels()
- {
- FW_ASSERT(fPixMapHandle != NULL);
- FW_ASSERT(fLockCount != 0);
-
- ::UnlockPixels(fPixMapHandle);
- if (--fLockCount == 0)
- fPixMapHandle = NULL;
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::Flatten
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::Flatten(FW_CWritableStream& archive) const
- {
- WriteToPlatformFormat(archive, TRUE);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::IsEqual
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CBitmapRep::IsEqual(const FW_CGraphicCountedPtrRep* other) const
- {
- if (other == this)
- return TRUE;
-
- #ifdef FW_DEBUG
- FW_CBitmapRep* bitmapRep = FW_DYNAMIC_CAST(FW_CBitmapRep, other);
- FW_ASSERT(bitmapRep != NULL);
- #else
- FW_CBitmapRep* bitmapRep = (FW_CBitmapRep*)other;
- #endif
-
- return fPlatformBitmap == bitmapRep->fPlatformBitmap;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::CopyPixels
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::CopyPixels(FW_PBitmap dstBitmap,
- const FW_CRect& srcRect,
- const FW_CRect& dstRect) const
- {
- FW_ASSERT((const void*)dstBitmap != NULL);
-
- FW_SPlatformRect plfmSrcRect = srcRect;
- FW_SPlatformRect plfmDstRect = dstRect;
-
- #ifdef FW_BUILD_MAC
- FW_CMacPixelLock srcLock((FW_CBitmapRep*)this);
- PixMapHandle srcPixMap = srcLock.GetPixMapHandle();
-
- FW_CMacPixelLock dstLock(dstBitmap.operator->());
- PixMapHandle dstPixMap = dstLock.GetPixMapHandle();
-
- CGrafPtr curPort;
- GDHandle gdh;
- ::GetGWorld(&curPort, &gdh);
- ::SetGWorld(dstBitmap->GetPlatformBitmap(), NULL);
- ::CopyBits((BitMap*)*srcPixMap, (BitMap*)*dstPixMap,
- &plfmSrcRect, &plfmDstRect,
- srcCopy, NULL);
- ::SetGWorld(curPort, gdh);
- #endif
- #ifdef FW_BUILD_WIN
- HBITMAP dstBitmapHandle = dstBitmap->GetPlatformBitmap();
- HPALETTE hDstPalette = dstBitmap->GetPalette();
-
- FW_CPrivWinBitmap::CopyImage(
- dstBitmapHandle, hDstPalette, plfmDstRect,
- fPlatformBitmap, fWinPalette, plfmSrcRect,
- FW_CPrivWinBitmap::kScale);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::GetBitmapInfo
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::GetBitmapInfo(short& width,
- short& height,
- short& rowBytes,
- short& pixelSize) const
- {
- FW_ASSERT(fPlatformBitmap != NULL);
-
- #ifdef FW_BUILD_MAC
- PixMapHandle pixMap = ::GetGWorldPixMap(fPlatformBitmap);
-
- width = (*pixMap)->bounds.right - (*pixMap)->bounds.left;
- height = (*pixMap)->bounds.bottom - (*pixMap)->bounds.top;
- rowBytes = (*pixMap)->rowBytes ^ 0x8000;
- pixelSize = (*pixMap)->pixelSize;
- #endif
-
- #ifdef FW_BUILD_WIN
- BITMAP bm;
- ::GetObject(fPlatformBitmap, sizeof(bm), &bm);
-
- width = bm.bmWidth;
- height = bm.bmHeight;
- rowBytes = bm.bmWidthBytes;
- pixelSize = fUserColorDepth; // The actual DDB pixel size may differ
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::GetBitmapBounds
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::GetBitmapBounds(FW_CRect& bounds) const
- {
- short width, height,rowBytes, pixelSize;
- GetBitmapInfo(width, height, rowBytes, pixelSize);
-
- bounds.SetInt(0, 0, width, height);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::GetBitmapBounds
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::GetBitmapBounds(FW_CGraphicContext& gc, FW_CRect& bounds) const
- {
- short width, height,rowBytes, pixelSize;
- GetBitmapInfo(width, height, rowBytes, pixelSize);
-
- FW_CPoint size = gc.DeviceToLogical(width, height);
- bounds.Set(FW_IntToFixed(0), FW_IntToFixed(0), size.x, size.y);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::ChangeBitmap
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::ChangeBitmap(void* image, short imageSize,
- short width, short height,
- short pixelSize,
- short howToChangeSize)
- {
- FW_ASSERT(fPlatformBitmap != NULL);
-
- #ifdef FW_BUILD_MAC
- FW_ASSERT(!MacIsPixelsLocked());
- FW_SPlatformRect boundsRect(0, 0, width, height);
- ::UpdateGWorld(&fPlatformBitmap, pixelSize, &boundsRect, NULL, NULL,
- howToChangeSize == kChangeBitmap_Crop ? clipPix : stretchPix);
- #endif
-
- #ifdef FW_BUILD_WIN
- // Determine color depth for the DDB
- short bmNewPlanes = 1, bmNewBitsPixel = 1;
- if(pixelSize != 1)
- FW_WinPrivGetDisplayColorInfo(bmNewPlanes, bmNewBitsPixel);
-
- // Get the current bitmap depth
- BITMAP bm;
- ::GetObject(fPlatformBitmap, sizeof(bm), &bm);
-
- if(bm.bmPlanes != bmNewPlanes || bm.bmBitsPixel != bmNewBitsPixel ||
- bm.bmWidth != width || bm.bmHeight != height)
- {
- // Create a new bitmap
- HBITMAP bitmap = ::CreateBitmap(width, height, bmNewPlanes, bmNewBitsPixel, NULL);
- if(bitmap == NULL)
- FW_Failure(FW_xMemoryExhausted);
-
- // Copy the bits
- FW_SPlatformRect rectSrc(0, 0, bm.bmWidth, bm.bmHeight);
- FW_SPlatformRect rectDst(0, 0, width, height);
- FW_CPrivWinBitmap::CopyImage(
- bitmap, NULL, rectDst, // Even if fWinPalette is not NULL,
- fPlatformBitmap, NULL, rectSrc, // no color translation is needed here
- howToChangeSize == kChangeBitmap_Crop
- ? FW_CPrivWinBitmap::kCrop
- : FW_CPrivWinBitmap::kScale);
-
- // Replace the platform bitmap
- AdoptPlatformBitmap(bitmap);
- }
-
- // Update the color depth
- if(pixelSize != 0)
- fUserColorDepth = pixelSize;
- else
- fUserColorDepth = FW_CPrivWinBitmap::GetColorDepth(fPlatformBitmap);
- #endif
-
- if (image != NULL)
- SetImage(image, imageSize);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::Read
- //----------------------------------------------------------------------------------------
-
- void* FW_CBitmapRep::Read(FW_CReadableStream& archive)
- {
- FW_CBitmapRep* objectRep = new FW_CBitmapRep(archive, TRUE);
- return objectRep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::Copy
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap FW_CBitmapRep::Copy() const
- {
- FW_CRect bounds;
- GetBitmapBounds(bounds);
- return Copy(bounds);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::OrphanPlatformBitmap
- //----------------------------------------------------------------------------------------
-
- FW_PlatformBitmap FW_CBitmapRep::OrphanPlatformBitmap()
- {
- // Test first that we own fPlatformBitmap
- FW_ASSERT(fOwnBitmap == TRUE);
- fOwnBitmap = FALSE;
- return fPlatformBitmap;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::SetPlatformBitmap
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::SetPlatformBitmap(FW_PlatformBitmap newBitmap)
- {
- if (fPlatformBitmap != newBitmap)
- {
- DisposePlatformBitmap();
- fPlatformBitmap = newBitmap;
- }
-
- fOwnBitmap = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::AdoptPlatformBitmap
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::AdoptPlatformBitmap(FW_PlatformBitmap newBitmap)
- {
- SetPlatformBitmap(newBitmap);
- fOwnBitmap = TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::Copy
- //----------------------------------------------------------------------------------------
-
- FW_PBitmap FW_CBitmapRep::Copy(const FW_CRect& srcRect) const
- {
- FW_CRect bounds;
- GetBitmapBounds(bounds);
-
- FW_CRect clipRect = srcRect;
-
- if (clipRect.left < FW_kFixed0)
- clipRect.left = FW_kFixed0;
- else if (clipRect.left > bounds.right)
- clipRect.left = bounds.right;
-
- if (clipRect.top < FW_kFixed0)
- clipRect.top = FW_kFixed0;
- else if (clipRect.top > bounds.bottom)
- clipRect.top = bounds.bottom;
-
- if (clipRect.right > bounds.right)
- clipRect.right = bounds.right;
- else if (clipRect.right < clipRect.left)
- clipRect.right = clipRect.left;
-
- if (clipRect.bottom > bounds.bottom)
- clipRect.bottom = bounds.bottom;
- else if (clipRect.bottom < clipRect.top)
- clipRect.bottom = clipRect.top;
-
- FW_CRect dstRect(FW_kFixed0, FW_kFixed0, clipRect.right - clipRect.left, clipRect.bottom - clipRect.top);
-
- short width, height,rowBytes, pixelSize;
- GetBitmapInfo(width, height, rowBytes, pixelSize);
-
- FW_Palette palette = GetPalette();
- FW_PBitmap bitmap(dstRect.right.AsInt(), dstRect.bottom.AsInt(), pixelSize, palette);
- CopyPixels(bitmap, clipRect, dstRect);
-
- return bitmap;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::GetPalette
- //----------------------------------------------------------------------------------------
-
- FW_Palette FW_CBitmapRep::GetPalette() const
- {
- #ifdef FW_BUILD_MAC
- FW_CMacPixelLock lock((FW_CBitmapRep*) this);
- PixMapHandle pmh = lock.GetPixMapHandle();
- return (*pmh)->pmTable;
- #endif
- #ifdef FW_BUILD_WIN
- return fWinPalette;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::SetPalette
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::SetPalette(FW_Palette palette)
- {
- #ifdef FW_BUILD_MAC
- FW_CMacPixelLock lock(this);
- PixMapHandle pmh = lock.GetPixMapHandle();
- CTabHandle oldTable = (*pmh)->pmTable;
- (*pmh)->pmTable = palette;
- ::DisposeHandle((Handle)oldTable);
- #endif
- #ifdef FW_BUILD_WIN
- if (fWinPalette != NULL)
- ::DeleteObject(fWinPalette);
- fWinPalette = palette;
- #endif
- }
-
- #ifdef FW_BUILD_MAC
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::GetAsPicture
- //----------------------------------------------------------------------------------------
-
- FW_PPicture FW_CBitmapRep::GetAsPicture()
- {
- FW_CRect bounds;
- GetBitmapBounds(bounds);
- return GetAsPicture(bounds);
- }
-
- #endif
-
- #ifdef FW_BUILD_MAC
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::GetAsPicture
- //----------------------------------------------------------------------------------------
-
- FW_PPicture FW_CBitmapRep::GetAsPicture(const FW_CRect& srcRect)
- {
- CGrafPtr curPort;
- GDHandle gdh;
- ::GetGWorld(&curPort, &gdh);
-
- FW_CMacPixelLock lock(this);
- PixMapHandle pixMap = lock.GetPixMapHandle();
-
- GDHandle gdevice = ::GetGWorldDevice(fPlatformBitmap);
-
- ::SetGWorld(fPlatformBitmap, gdevice);
-
- FW_SPlatformRect plfmSrcRect = srcRect;
- FW_SPlatformRect plfmDstRect = srcRect;
- ::OffsetRect(&plfmDstRect, -plfmDstRect.left, -plfmDstRect.top);
-
- OpenCPicParams openParams;
- openParams.srcRect = plfmDstRect;
- openParams.hRes =
- openParams.vRes = 0x00480000;
- openParams.version = -2;
- openParams.reserved1 =
- openParams.reserved2 = 0;
-
- PicHandle hPict = ::OpenCPicture(&openParams);
- ::CopyBits((BitMap*)*pixMap, (BitMap*)*pixMap,
- &plfmSrcRect, &plfmDstRect,
- srcCopy, NULL);
- ::ClosePicture();
-
- ::SetGWorld(curPort, gdh);
-
- return FW_PPicture(hPict);
- }
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::WriteToPlatformFormat
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::WriteToPlatformFormat(const FW_CWritableStream& stream, FW_Boolean dibFileHeader) const
- {
- #ifdef FW_BUILD_MAC
- // Lock the GWorld pixmap
- FW_CMacPixelLock lock((FW_CBitmapRep*)this);
- PixMapHandle pmh = lock.GetPixMapHandle();
-
- short width = (*pmh)->bounds.right;
- short height = (*pmh)->bounds.bottom;
- short bitDepth = (*pmh)->pixelSize;
-
- short resRowBytes = ((width * bitDepth + 31) & ~31) / 8;
- short memRowBytes = (*pmh)->rowBytes & 0x7FFF;
-
- FW_ASSERT(resRowBytes <= memRowBytes);
-
- long imageSize = height * resRowBytes;
-
- // Determine color table size
- int colorTableCount = bitDepth < 16 ? 1 << bitDepth : 0;
- int colorTableSize = colorTableCount * sizeof(RGBQUAD);
-
- // Create a file header
- if(dibFileHeader)
- {
- BITMAPFILEHEADER bmfh;
- bmfh.bfType = FW_kWinBitmapSignature;
- bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
- colorTableSize + imageSize;
- bmfh.bfReserved1 = 0;
- bmfh.bfReserved2 = 0;
- bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
- colorTableSize;
-
- // Byte-swap and write it out
-
- MacRepackValue(&bmfh.bfType);
- MacRepackValue(&bmfh.bfSize);
- MacRepackValue(&bmfh.bfReserved1);
- MacRepackValue(&bmfh.bfReserved2);
- MacRepackValue(&bmfh.bfOffBits);
-
- stream.Write(&bmfh, sizeof(bmfh));
- }
-
- // Create a DIB header
-
- BITMAPINFOHEADER bmih;
-
- bmih.biSize = sizeof(BITMAPINFOHEADER);
- bmih.biWidth = width;
- bmih.biHeight = height;
- bmih.biPlanes = 1;
- bmih.biBitCount = bitDepth;
-
- bmih.biCompression = 0;
- bmih.biSizeImage = imageSize;
-
- bmih.biXPelsPerMeter = 0;
- bmih.biYPelsPerMeter = 0;
- bmih.biClrUsed = 0;
- bmih.biClrImportant = 0;
-
- // Byte-swap and write it out
-
- MacRepackValue(&bmih.biSize);
- MacRepackValue(&bmih.biWidth);
- MacRepackValue(&bmih.biHeight);
- MacRepackValue(&bmih.biPlanes);
- MacRepackValue(&bmih.biBitCount);
- MacRepackValue(&bmih.biCompression);
- MacRepackValue(&bmih.biSizeImage);
- MacRepackValue(&bmih.biXPelsPerMeter);
- MacRepackValue(&bmih.biYPelsPerMeter);
- MacRepackValue(&bmih.biClrUsed);
- MacRepackValue(&bmih.biClrImportant);
-
- stream.Write(&bmih, sizeof(bmih));
-
- // Write the color table (if applicable)
- if(colorTableCount != 0)
- {
- CTabHandle cTabHandle = (*pmh)->pmTable;
- FW_ASSERT(colorTableCount <= (*cTabHandle)->ctSize + 1); // ctSize is count - 1!
-
- #ifdef FW_SHOW_CTABS
- FW_MacShowColorTable(cTabHandle);
- #endif
-
- for(int i = 0; i < colorTableCount; ++i)
- {
- RGBQUAD quad;
-
- quad.rgbBlue = (*cTabHandle)->ctTable[i].rgb.blue >> 8;
- quad.rgbGreen = (*cTabHandle)->ctTable[i].rgb.green >> 8;
- quad.rgbRed = (*cTabHandle)->ctTable[i].rgb.red >> 8;
- quad.rgbReserved = 0;
-
- stream.Write(&quad, sizeof(quad));
- }
- }
-
- // Finally, do the bits (upside-down)
- char* bitsBuffer = new char[memRowBytes];
-
- const char* baseBuffer = ::GetPixBaseAddr(pmh);
- const char* currBuffer = baseBuffer + memRowBytes * (height - 1);
-
- for(int row = height - 1; row >= 0; -- row, currBuffer -= memRowBytes)
- {
- ::BlockMove(currBuffer, bitsBuffer, resRowBytes);
- MacRepackResourceRow(bitsBuffer, bitDepth, resRowBytes);
- stream.Write(bitsBuffer, resRowBytes);
- }
-
- delete[] bitsBuffer;
- #endif
-
- #ifdef FW_BUILD_WIN
- // Convert to DIB first; use the color depth the user wanted
- FW_CPrivWinDIB::HDIB dib = FW_CPrivWinDIB::ConvertFromBitmap(fPlatformBitmap, fUserColorDepth, fWinPalette);
-
- FW_TRY
- {
- // Write out the DIB
- FW_CPrivWinDIB::SaveToStream(stream, dib, dibFileHeader);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- FW_CPrivWinDIB::Free(dib);
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- // Free the DIB
- FW_CPrivWinDIB::Free(dib);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapRep::ReadFromPlatformFormat
- //----------------------------------------------------------------------------------------
-
- void FW_CBitmapRep::ReadFromPlatformFormat(const FW_CReadableStream& stream, FW_Boolean dibFileHeader)
- {
- #ifdef FW_BUILD_WIN
- FW_CPrivWinDIB::HDIB dib = FW_CPrivWinDIB::LoadFromStream(stream, dibFileHeader);
-
- // Get the palette
- fWinPalette = FW_CPrivWinDIB::GetPalette(dib);
-
- FW_TRY
- {
- fPlatformBitmap = FW_CPrivWinDIB::ConvertToBitmap(dib, fWinPalette);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- FW_CPrivWinDIB::Free(dib);
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- FW_CPrivWinDIB::Free(dib);
-
- // Update the color depth
- fUserColorDepth = FW_CPrivWinBitmap::GetColorDepth(fPlatformBitmap);
- #endif
-
- #ifdef FW_BUILD_MAC
-
- // Read the BITMAPFILEINFO header
- if(dibFileHeader)
- {
- BITMAPFILEHEADER bmfh;
-
- stream.Read((void*) &bmfh, sizeof(bmfh));
-
- MacRepackValue(&bmfh.bfType);
- MacRepackValue(&bmfh.bfSize);
- MacRepackValue(&bmfh.bfReserved1);
- MacRepackValue(&bmfh.bfReserved2);
- MacRepackValue(&bmfh.bfOffBits);
-
- if(bmfh.bfType != FW_kWinBitmapSignature || bmfh.bfReserved1 != 0 || bmfh.bfReserved2 != 0)
- FW_Failure(FW_xInvalidBitmapData);
- }
-
- // Start reading the header
- FW_Boolean bIsWindowsBitmap;
- BITMAPINFOHEADER bmih;
-
- stream.Read((void*) &bmih.biSize, sizeof(bmih.biSize));
-
- MacRepackValue(&bmih.biSize);
-
- if(bmih.biSize == sizeof(BITMAPINFOHEADER)) // Windows bitmap
- {
- // Read the Windows header
- stream.Read((void*) &bmih.biWidth, sizeof(bmih) - sizeof(bmih.biSize));
-
- MacRepackValue(&bmih.biWidth);
- MacRepackValue(&bmih.biHeight);
- MacRepackValue(&bmih.biPlanes);
- MacRepackValue(&bmih.biBitCount);
- MacRepackValue(&bmih.biCompression);
- MacRepackValue(&bmih.biSizeImage);
- MacRepackValue(&bmih.biXPelsPerMeter);
- MacRepackValue(&bmih.biYPelsPerMeter);
- MacRepackValue(&bmih.biClrUsed);
- MacRepackValue(&bmih.biClrImportant);
-
- bIsWindowsBitmap = TRUE;
- }
- else if(bmih.biSize == sizeof(BITMAPCOREHEADER)) // OS/2 bitmap
- {
- // Read the OS/2 header
- BITMAPCOREHEADER bmch;
-
- stream.Read((void* )&bmch.bcWidth, sizeof(bmch) - sizeof(bmch.bcSize));
-
- MacRepackValue(&bmch.bcWidth);
- MacRepackValue(&bmch.bcHeight);
- MacRepackValue(&bmch.bcPlanes);
- MacRepackValue(&bmch.bcBitCount);
-
- // Plug the data into the Windows header
- bmih.biWidth = bmch.bcWidth;
- bmih.biHeight = bmih.biHeight;
- bmih.biPlanes = bmch.bcPlanes;
- bmih.biBitCount = bmch.bcBitCount;
- bmih.biCompression = 0;
- bmih.biSizeImage = 0;
- bmih.biXPelsPerMeter = 0;
- bmih.biYPelsPerMeter = 0;
- bmih.biClrUsed = 0;
- bmih.biClrImportant = 0;
-
- bIsWindowsBitmap = FALSE;
- }
- else
- FW_Failure(FW_xInvalidBitmapData);
-
- // Check for compression flag -- we don't support it!
- if(bmih.biCompression != 0)
- FW_Failure(FW_xInvalidBitmapData);
-
- // Determine the size of the color table
- short bitDepth = bmih.biBitCount;
- short colorCount = bmih.biClrUsed != 0 ? bmih.biClrUsed : bitDepth >= 16 ? 0 : (1 << bitDepth);
-
- // Read the color table
- CTabHandle colorTabHandle = NULL;
- if(colorCount != 0)
- {
- Size colorTableSize = sizeof(ColorTable) + sizeof(ColorSpec) * (colorCount - 1);
- FW_CAcquireTemporarySystemHandle colorTableMemory(colorTableSize);
- CTabPtr colorTablePtr = (CTabPtr) colorTableMemory.GetPointer();
-
- for(int i = 0; i < colorCount; colorTablePtr->ctTable[i].value = i, ++i)
- {
- if(bIsWindowsBitmap)
- {
- RGBQUAD quad;
- stream.Read((void*) &quad, sizeof(quad));
-
- colorTablePtr->ctTable[i].rgb.red = (quad.rgbRed << 8) | quad.rgbRed;
- colorTablePtr->ctTable[i].rgb.green = (quad.rgbGreen << 8) | quad.rgbGreen;
- colorTablePtr->ctTable[i].rgb.blue = (quad.rgbBlue << 8) | quad.rgbBlue;
- }
- else
- {
- RGBTRIPLE trip;
- stream.Read((void*) &trip, sizeof(trip));
-
- colorTablePtr->ctTable[i].rgb.red = trip.rgbtRed << 8;
- colorTablePtr->ctTable[i].rgb.green = trip.rgbtGreen<< 8;
- colorTablePtr->ctTable[i].rgb.blue = trip.rgbtBlue << 8;
- }
- }
-
- colorTablePtr->ctSeed = 0;
- colorTablePtr->ctFlags = 0;
- colorTablePtr->ctSize = colorCount - 1;
-
- colorTabHandle = (CTabHandle) colorTableMemory.GetPlatformHandle();
- colorTableMemory.Orphan();
-
- ::CTabChanged(colorTabHandle);
-
- #ifdef FW_SHOW_CTABS
- FW_MacShowColorTable(colorTabHandle);
- #endif
- }
-
- // Create a GWorld
- FW_SPlatformRect bounds(0, 0, bmih.biWidth, bmih.biHeight);
- OSErr err = ::NewGWorld(&fPlatformBitmap, bitDepth, &bounds,
- colorTabHandle, NULL, useTempMem);
-
- if (colorTabHandle != NULL)
- ::DisposeCTable(colorTabHandle);
-
- FW_FailOnError(err);
-
- // Lock the memory pixmap
- FW_CMacPixelLock lock(this);
- PixMapHandle memPixMap = lock.GetPixMapHandle();
- void* memBaseAddr = ::GetPixBaseAddr(memPixMap);
-
- // Read the bits
- short resRowBytes = ((bmih.biWidth * bitDepth + 31) & ~31) / 8;
- short memRowBytes = (*memPixMap)->rowBytes & 0x7FFF;
- FW_ASSERT(resRowBytes <= memRowBytes);
-
- char* curBuffer = (char*) memBaseAddr + (bmih.biHeight - 1) * memRowBytes;
-
- for(int row = bmih.biHeight - 1; row >= 0; -- row, curBuffer -= memRowBytes)
- {
- // Read and repack the next row
- stream.Read(curBuffer, resRowBytes);
- MacRepackResourceRow(curBuffer, bitDepth, resRowBytes);
- }
- #endif
- }
-
- //========================================================================================
- // class FW_CBitmapContext
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapContext::FW_CBitmapContext
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapContext::FW_CBitmapContext(Environment* ev, FW_PBitmap& bitmap) :
- FW_CGraphicContext(ev),
- fBitmap(bitmap),
- fGraphicDevice(NULL)
- {
- FW_PlatformBitmap platformBitmap = bitmap->GetPlatformBitmap();
- ODPlatformCanvas platformCanvas;
-
- #ifdef FW_BUILD_WIN
- platformCanvas = ::CreateCompatibleDC(NULL);
- fOldBitmap = (HBITMAP) ::SelectObject(platformCanvas, platformBitmap);
-
- if(fOldBitmap == NULL)
- FW_Failure(FW_xGraphicException);
- #endif
- #ifdef FW_BUILD_MAC
- platformCanvas = (ODPlatformCanvas) platformBitmap;
- bitmap->MacLockPixels();
- #endif
-
- FW_CRect bounds;
- bitmap->GetBitmapBounds(bounds);
-
- fGraphicDevice = new FW_CGraphicDevice(ev, platformCanvas);
- fGraphicDevice->SetResolution(FW_CPoint(FW_IntToFixed(72), FW_IntToFixed(72)));
-
- FW_CAcquiredODShape aqClipShape = ::FW_NewODShape(ev, bounds);
-
- InitGraphicContext(fGraphicDevice,
- NULL,
- aqClipShape);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CBitmapContext::~FW_CBitmapContext
- //----------------------------------------------------------------------------------------
-
- FW_CBitmapContext::~FW_CBitmapContext()
- {
- FW_START_DESTRUCTOR
-
- #ifdef FW_BUILD_MAC
- fBitmap->MacUnlockPixels();
- #endif
-
- #ifdef FW_BUILD_WIN
- // Save the DC for later use
- HDC hdcBitmap = fGraphicDevice->GetPlatformCanvas();
- #endif
-
- // ----- Terminate the superclass
- TerminateGraphicContext();
-
- #ifdef FW_BUILD_WIN
- ::SelectObject(hdcBitmap, fOldBitmap);
- ::DeleteDC(hdcBitmap);
- #endif
-
- // ----- Delete the device
- fGraphicDevice->Release();
- }
-
- #ifdef FW_BUILD_MAC
-
- //========================================================================================
- // CLASS FW_CMacPixelLock
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CMacPixelLock::FW_CMacPixelLock
- //----------------------------------------------------------------------------------------
-
- FW_CMacPixelLock::FW_CMacPixelLock(FW_CBitmapRep* bitmapRep) :
- fBitmapRep(bitmapRep)
- {
- fPixMapHandle = fBitmapRep->MacLockPixels();
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMacPixelLock::~FW_CMacPixelLock
- //----------------------------------------------------------------------------------------
-
- FW_CMacPixelLock::~FW_CMacPixelLock()
- {
- FW_START_DESTRUCTOR
-
- fBitmapRep->MacUnlockPixels();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMacPixelLock::GetPixMapHandle
- //----------------------------------------------------------------------------------------
-
- PixMapHandle FW_CMacPixelLock::GetPixMapHandle() const
- {
- return fPixMapHandle;
- }
-
- #endif
-